package br.com.beautycenter.view;

import br.com.beautycenter.Funcionalidades.Bean;
import br.com.beautycenter.Funcionalidades.Swing;
import br.com.beautycenter.Funcionalidades.Validacoes;
import br.com.beautycenter.DAO.PersisteFuncionario;
import br.com.beautycenter.DAO.PersisteSuprimento;
import br.com.beautycenter.bean.Funcionario;
import br.com.beautycenter.bean.Suprimento;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.swing.JOptionPane;

public final class CadastroSuprimento extends javax.swing.JFrame {
    Funcionario funcionario = new Funcionario();
    PersisteFuncionario persisteFuncionario = new PersisteFuncionario();
    PersisteSuprimento persisteSuprimento = new PersisteSuprimento();
    
    Suprimento suprimentoTemp = new Suprimento();
    Suprimento suprimento = new Suprimento();  
    
    static List<Suprimento> listaSuprimento = new ArrayList<Suprimento>();
    
    public CadastroSuprimento() {
        initComponents();
        iniciarTela();
    }
    
    public void iniciarTela(){
        preencherComboFuncionario();
        limparCampos();
        desativarCampos();
        Validacoes.inicializarMascaras();
        ativarBotoes();
        
        if(listaSuprimento.isEmpty()){
            ativarCampos();
        }
    }
    
    public void novo(){
        limparCampos();
        ativarCampos();
        
        btnNovo.setEnabled(false);
        btnSalvar.setEnabled(true);
        btnCancelar.setEnabled(true);
        
        //cria um novo Suprimento
        suprimento = new Suprimento();
    }
    
    public void preencherComboFuncionario(){
        if(jComboFuncionario.getItemCount() > 1)
            return;
            for(Funcionario item: persisteFuncionario.retornarFuncionario()){
                jComboFuncionario.addItem(item.getNome());
            }
    }
    
    public boolean validarValor(){
        Double valor = Validacoes.validarCampoMoeda(txtValor.getText().trim());
        
        if(valor != null){
           suprimentoTemp.setValorSuprimento(valor);
           return true;
        }else{
           JOptionPane.showMessageDialog(null, "Valor informado é invalido!");
           txtValor.requestFocus();
           return false;
        }
    }
    
    public boolean validarDescricao(){
        if(!Validacoes.validarCampoTexto(txtDescricao.getText().trim())){
            JOptionPane.showMessageDialog(null, "Campo descrição é obrigatório!");
            txtDescricao.requestFocus();
            return false;
        }

        suprimentoTemp.setDescricaoSuprimento(txtDescricao.getText().trim());
        return true;
    }
    
    public boolean validarFuncionario(){
        int selecionado = jComboFuncionario.getSelectedIndex();
        
        //se não for o primeiro, então é válido
        if(selecionado > 0){
           funcionario = new Funcionario();
           funcionario.setNome(jComboFuncionario.getSelectedItem().toString());
           funcionario = persisteFuncionario.retornarFuncionarioPeloNome(funcionario).get(0);
           
           if(funcionario != null){
               suprimentoTemp.setFuncionario(funcionario);
               return true;
           }else{
                /**
                 * Caso o funcionario não esteja cadastrado no banco de dados, entrará aqui.
                 * Isso provavelmente não acontecerá, mas é bom deixar um tratamento.
                 */               
               JOptionPane.showMessageDialog(null, "Funcionario não cadastrado.");
               return false;
           }
           
        }
        
        JOptionPane.showMessageDialog(null, "Selecione um funcionario");
        jComboFuncionario.requestFocus();
        return false;
    }

    
    public boolean preencherSuprimento(){
        
        suprimentoTemp = new Suprimento();
        
        if(validarValor() && validarDescricao() && validarFuncionario()){
           
            //Pega a data do sistema operacional
            Date date = new Date();  
            suprimentoTemp.setDataCadastro(date);
            
            suprimento = new Suprimento();
            
            Bean.copiarSuprimento(suprimentoTemp, suprimento);

            persisteSuprimento.salvar(suprimento);
            
            JOptionPane.showMessageDialog(null, "Suprimento cadastrado com sucesso");
            return true;
        }
        
        return false;
    }
    
    public void limparCampos(){
        Swing.zerarCampos(jPanelPrincipal.getComponents());
        txtDescricao.setText("");
    }
    
    public void ativarCampos(){
        Swing.ativarCampos(jPanelPrincipal.getComponents());
        txtDescricao.enable();
        txtValor.requestFocus();
    }
    
    public void desativarCampos(){
        Swing.desativarCampos(jPanelPrincipal.getComponents());
        txtDescricao.disable();
    }
    
    public void ativarBotoes(){
        listaSuprimento = persisteSuprimento.retornarSuprimento();
        
        if(listaSuprimento.isEmpty()){
            btnNovo.setEnabled(false);
            btnSalvar.setEnabled(true);
            btnCancelar.setEnabled(true);
            ativarCampos();
        }else{
            btnNovo.setEnabled(true);
            btnSalvar.setEnabled(false);
            btnCancelar.setEnabled(false);
        }
    }    
    
    public void cancelar(){
        limparCampos();
        desativarCampos();
        ativarBotoes();
        txtValor.requestFocus();
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelPrincipal = new javax.swing.JPanel();
        jPanelBotoes = new javax.swing.JPanel();
        btnSair = new javax.swing.JButton();
        btnSalvar = new javax.swing.JButton();
        btnCancelar = new javax.swing.JButton();
        btnNovo = new javax.swing.JButton();
        jLabelValor = new javax.swing.JLabel();
        txtValor = new javax.swing.JTextField();
        jLabelFuncionario = new javax.swing.JLabel();
        jComboFuncionario = new javax.swing.JComboBox();
        jLabelDescricao = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        txtDescricao = new javax.swing.JTextArea();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("ERP BEAUTY CENTER - Suprimento");
        setPreferredSize(new java.awt.Dimension(500, 230));

        jPanelPrincipal.setBackground(new java.awt.Color(255, 255, 255));
        jPanelPrincipal.setPreferredSize(new java.awt.Dimension(500, 200));

        jPanelBotoes.setPreferredSize(new java.awt.Dimension(500, 45));

        btnSair.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Sair.png"))); // NOI18N
        btnSair.setToolTipText("SAIR");
        btnSair.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSairActionPerformed(evt);
            }
        });

        btnSalvar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Salvar.png"))); // NOI18N
        btnSalvar.setToolTipText("SALVAR");
        btnSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSalvarActionPerformed(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Cancelar.png"))); // NOI18N
        btnCancelar.setToolTipText("LIMPAR");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnNovo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/br/com/beautycenter/img/Novo.png"))); // NOI18N
        btnNovo.setToolTipText("NOVO");
        btnNovo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnNovoActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelBotoesLayout = new javax.swing.GroupLayout(jPanelBotoes);
        jPanelBotoes.setLayout(jPanelBotoesLayout);
        jPanelBotoesLayout.setHorizontalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelBotoesLayout.createSequentialGroup()
                .addContainerGap(114, Short.MAX_VALUE)
                .addComponent(btnNovo)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSalvar)
                .addGap(5, 5, 5)
                .addComponent(btnCancelar)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(btnSair)
                .addGap(109, 109, 109))
        );
        jPanelBotoesLayout.setVerticalGroup(
            jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelBotoesLayout.createSequentialGroup()
                .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelBotoesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnSalvar)
                        .addComponent(btnCancelar)
                        .addComponent(btnSair, javax.swing.GroupLayout.Alignment.TRAILING))
                    .addComponent(btnNovo))
                .addGap(0, 4, Short.MAX_VALUE))
        );

        jLabelValor.setText("VALOR:");

        jLabelFuncionario.setText("FUNCIONARIO");

        jComboFuncionario.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "" }));

        jLabelDescricao.setText("DESCRIÇÃO:");

        txtDescricao.setColumns(20);
        txtDescricao.setRows(5);
        jScrollPane1.setViewportView(txtDescricao);

        javax.swing.GroupLayout jPanelPrincipalLayout = new javax.swing.GroupLayout(jPanelPrincipal);
        jPanelPrincipal.setLayout(jPanelPrincipalLayout);
        jPanelPrincipalLayout.setHorizontalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabelDescricao)
                    .addComponent(jLabelValor))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                        .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jLabelFuncionario)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jComboFuncionario, 0, 199, Short.MAX_VALUE))
                    .addComponent(jScrollPane1))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanelPrincipalLayout.setVerticalGroup(
            jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                .addComponent(jPanelBotoes, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelValor)
                    .addComponent(txtValor, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabelFuncionario)
                    .addComponent(jComboFuncionario, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelPrincipalLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelPrincipalLayout.createSequentialGroup()
                        .addComponent(jLabelDescricao)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelPrincipalLayout.createSequentialGroup()
                        .addGap(0, 3, Short.MAX_VALUE)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(24, 24, 24))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jPanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 0, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelPrincipal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnSairActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSairActionPerformed
        dispose();
    }//GEN-LAST:event_btnSairActionPerformed

    private void btnSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSalvarActionPerformed
        if(preencherSuprimento()){
            iniciarTela();
        }
    }//GEN-LAST:event_btnSalvarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        cancelar();
    }//GEN-LAST:event_btnCancelarActionPerformed

    private void btnNovoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnNovoActionPerformed
        novo();
    }//GEN-LAST:event_btnNovoActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnNovo;
    private javax.swing.JButton btnSair;
    private javax.swing.JButton btnSalvar;
    private javax.swing.JComboBox jComboFuncionario;
    private javax.swing.JLabel jLabelDescricao;
    private javax.swing.JLabel jLabelFuncionario;
    private javax.swing.JLabel jLabelValor;
    private javax.swing.JPanel jPanelBotoes;
    private javax.swing.JPanel jPanelPrincipal;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTextArea txtDescricao;
    private javax.swing.JTextField txtValor;
    // End of variables declaration//GEN-END:variables
}
